<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="架层编号" prop="caseNo">
        <el-input
          v-model="queryParams.caseNo"
          placeholder="请输入架层编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="架层名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入架层名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="书架" prop="posId">
        <el-select
          v-model="queryParams.posId"
          placeholder="请选择书架"
          clearable
          filterable
          remote
          :remote-method="remoteMethod"
          size="small"
          @clear="handleClear"
        >
          <el-option
            v-for="dict in posArr"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="small"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="small"
          @click="resetQuery"
        >重置</el-button>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" border :data="list">
      <el-table-column
        key="caseNo"
        label="架层编号"
        align="center"
        prop="caseNo"
      />
      <el-table-column
        key="caseName"
        label="架层名称"
        align="center"
        prop="caseName"
      />
      <el-table-column key="side" label="架层方向" align="center" prop="side" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon=""
            class="delcolor"
            @click="handleDel(scope.row)"
          >
            删除
          </el-button>
          <el-button
            size="small"
            type="text"
            icon=""
            @click="handleEdit(scope.row)"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[20, 40, 60, 80]"
      :page.sync="queryParams.page.current"
      :limit.sync="queryParams.page.size"
      @pagination="getListpagination"
    />
    <popCon
      :id="chosid"
      :visible.sync="show"
      :obj-target="objTarget"
      @close="close"
    />
  </div>
</template>

<script>
import pagination from '@/components/Pagination'
import popCon from './components/add.vue'
import { caseList, deleteCase } from '@/api/case'
import { positionArr } from '@/api/model'
export default {
  name: 'Home',
  components: {
    pagination,
    popCon
  },
  data() {
    return {
      queryParams: {
        name: '',
        caseNo: '',
        posId: '',
        page: {
          searchCount: true, // 是否显示搜索数量
          current: 1, // 当前页
          size: 20
        }
      },
      loading: false,
      list: [],
      total: 0,
      show: false,
      chosid: null,
      objTarget: '',
      posArr: []
    }
  },
  mounted() {
    this.requestpositionArr()
    this.handleQuery()
  },
  methods: {
    handleClear() {
      this.requestpositionArr('')
    },
    remoteMethod(query) {
      this.requestpositionArr(query)
    },
    requestpositionArr(name) {
      this.posArr = []
      positionArr('Shelf', name)
        .then((res) => {
          if (res.success) {
            const arr = res?.data ?? []
            if (arr.length > 0) {
              arr.map((item) => {
                const obj = {
                  label: item?.posName ?? '',
                  value: item?.posId ?? ''
                }
                this.posArr.push(obj)
              })
            }
          }
        })
        .catch((err) => {})
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList(true)
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        name: '',
        caseNo: '',
        posId: '',
        page: {
          searchCount: true, // 是否显示搜索数量
          current: 1, // 当前页
          size: 20
        }
      }
      this.handleQuery()
    },
    /** 查询菜单列表 */
    getList(falge) {
      this.loading = true
      this.queryParams.page.searchCount = falge
      caseList(this.queryParams).then((res) => {
        this.loading = false
        if (res.success) {
          this.list = res?.data ?? []
          if (falge || falge === undefined) {
            this.total = res?.page?.total ?? 0
          }
        }
      })
    },
    /* 分页查询参数 */
    getListpagination() {
      this.getList(false)
    },
    handleAdd() {
      this.show = true
      this.chosid = null
    },
    close(status) {
      this.show = false
      this.chosid = null
      setTimeout(() => {
        if (status) {
          this.handleQuery()
        }
      }, 200)
    },
    handleDel(row) {
      const text = '确定删除该定位信息?'
      this.$confirm(text, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.requestDel(row.caseId)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    requestDel(caseId) {
      deleteCase(caseId)
        .then((res) => {
          if (res.success) {
            this.handleQuery()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch((err) => {
          this.$message({
            type: 'error',
            message: '删除失败!'
          })
        })
    },
    handleEdit(row) {
      this.show = true
      this.chosid = row.caseId
      this.objTarget = JSON.stringify(row)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
